/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bookstore.action;

import bookstore.dao.BsBookDao;
import bookstore.iservice.IBookService;
import bookstore.model.Book;
import bookstore.service.BookService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.*;
import java.util.*;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author cjx
 */
public class UploadAction extends ActionSupport {

    private final int BUFFER_SIZE = 16 * 1024;
    private Book book;
    private String title;
    private File upload;
    private String uploadFileName;
    private String uploadContentType;
    private int list_id;

    public int getList_id() {
        return list_id;
    }

    public void setList_id(int list_id) {
        this.list_id = list_id;
    }
    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }
    private String uploadedFileName;

    public String getUploadedFileName() {
        return uploadedFileName;
    }

    @Override
    public String execute() throws Exception {
        String newFileName = new Date().getTime() + getExtention(uploadFileName);
        File picFile = new File(ServletActionContext.
                getServletContext().getRealPath("/bookimages"), newFileName);
        copy(upload, picFile);
        uploadedFileName = newFileName;
        book.setPicture(newFileName);
        IBookService bookService = new BookService();
        bookService.setBookDao(new BsBookDao());
        bookService.addBook(book);
        return SUCCESS;
    }
    public String change() throws Exception{
        String newFileName = new Date().getTime() + getExtention(uploadFileName);
        File picFile = new File(ServletActionContext.
                getServletContext().getRealPath("/bookimages"), newFileName);
        copy(upload, picFile);
        uploadedFileName = newFileName;
        book.setPicture(newFileName);
         ActionContext ctx = ActionContext.getContext();
         Map session = ctx.getSession();
         Book oldBook = ((List<Book>)session.get("allList")).get(list_id);
         System.out.println("11111111111111111111111111");
        System.out.println(book.getId()+book.getPicture()+list_id);
        System.out.println("11111111111111111111111111");
        if(book.getPicture() == null)
            book.setPicture(oldBook.getPicture());
        IBookService bookService = new BookService();
        bookService.setBookDao(new BsBookDao());
        bookService.updateBook(book);
        return SUCCESS;
    }
    private void copy(File src, File dst) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(src));
            out = new BufferedOutputStream(new FileOutputStream(dst));
            byte[] buffer = new byte[BUFFER_SIZE];
            while (in.read(buffer) > 0) {
                out.write(buffer);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw e;
                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    throw e;
                }
            }
        }
    }

    private String getExtention(String fileName) {
        int pos = fileName.lastIndexOf(".");
        return fileName.substring(pos);
    }

}
